<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/4/4
 * Time: 10:50
 */

namespace app\admin\logic;

use app\admin\logic\code\ErrorCode;
use app\common\model\Admin;
use app\common\model\Site;
use app\common\model\SiteTeam;
use think\Error;

class AdminLogic extends BaseLogic {

    private $adminModel;

    public function __construct()
    {
        parent::__construct();
        $this->adminModel = new Admin();
    }

    public function ajaxIndex($tempWhere, $nowPage){
        $where = [];
        $tempWhere['username'] && $where[] = ['name','like','%'.$tempWhere['username'].'%'];
        $adminCount = $this->adminModel->with('role')->where($where)->count();
        if($adminCount <= 0){
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }else{
            $adminList = $this->adminModel->with(['role','site','teams'])->hidden(['update_time', 'password', 'salt'])->page($nowPage, $tempWhere['limit'])->where($where)->order('id desc')->select();
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $adminList, $adminCount);
        }
    }
    //详情
    public function info($id){
        $info = Admin::get($id);
        return $info;
    }

    /**
     * 处理管理员逻辑
     * @param $data
     */
    public function pwd($data){
        $id = $_SESSION['xm_travel']['admin_info']['id'];
        if ($data['password']){
            $validate = new \app\admin\validate\Admin();
            if (!$validate->check($data)) {
                return ajaxArr(ErrorCode::$PARAMVALIDATE, $validate->getError());
            }
            $admin = $this->adminModel->get($id);
            $admin->password = pwdMd5($data['password'],$admin['salt']);
            $admin->update_time = time();
            $res = $admin->save();
            if ($res){
                return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
            } else{
                return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
            }
        }
    }
    public function handle($data){
        $act = $data['act'];
        $res = false;
        unset($data['act']);
        $validate = new \app\admin\validate\Admin();
        if ($act == 'add' || $act == 'edit'){
            if (!$validate->check($data)) {
                return ajaxArr(ErrorCode::$PARAMVALIDATE, $validate->getError());
            }
        }
        switch ($act){
            case 'add':
                $this->adminModel->name = $data['name'];
                $this->adminModel->status = $data['status'];
                $this->adminModel->role_id = $data['role_id'];
                $this->adminModel->site_id = $data['site_id'];
                $this->adminModel->password = $data['password'];
                $res = $this->adminModel->save();
                break;
            case 'edit':
                $admin = $this->adminModel->get($data['id']);
                if ($data['password']){
                    $admin->password = pwdMd5($data['password'],$admin['salt']);
                }
                $admin->name = $data['name'];
                $admin->status = $data['status'];
                $admin->role_id = $data['role_id'];
                $admin->site_id = $data['site_id'];
                $admin->oid = $data['oid'];
                if($data['oid']){
                    SiteTeam::where('id','eq',$data['oid'])->update(['name'=>$data['name']]);
                }
                if($data['site_id']){
                    Site::where('id','eq',$data['site_id'])->update(['name'=>$data['name']]);
                }
                $res = $admin->save();
                break;
            case 'del':
                $admin = $this->adminModel->get($data['id']);
                if (!$admin){
                    return ajaxArr(ErrorCode::$INFONULL, '该管理员不存在');
                }
                $res = $admin->delete();
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }
}